package org.demo.spring.data.custom;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Service
public class AccountDao {

    @Autowired
    private JDBCUtil jdbcUtil;


    public void add(){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sql = "UPDATE account SET money = money+ ? WHERE name = ?";
        try {
            connection = jdbcUtil.getConnection();
            System.out.println(connection);
            connection.setAutoCommit(false);
            preparedStatement =connection.prepareStatement(sql);
            preparedStatement.setInt(1,100);
            preparedStatement.setString(2,"aaa");
            preparedStatement.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null){
                    preparedStatement.close();
                }
                if(connection!=null){
                    connection.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}
